//
//  WmcTagColorSelectView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/17.
//

import Kingfisher
import UIKit
import YPBedrock

extension WmcTagColorSelectView {

    class ListModel {
        // Lifecycle

        init(color: WmcBaseTagView.Colors,
             isSelect: Bool)
        {
            self.color = color
            self.isSelect = isSelect
        }

        // Internal

        var color: WmcBaseTagView.Colors
        var isSelect: Bool
    }
}

// MARK: - [WmcTagColorSelectView]

class WmcTagColorSelectView: UICollectionView {
    // Lifecycle

    convenience init() {
        let frame = CGRect(x: 0, y: 0, width: Wmc_screenWidth, height: 74)
        self.init(frame: frame, collectionViewLayout: Self.layout)
        delegate = self
        dataSource = self
        self.yp.registerCells([
            YPRegister(WmcTagColorSelectCell.self),
        ])
        backgroundColor = .clear
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
        for (idx, color) in WmcBaseTagView.Colors.allCases.enumerated() {
            let model: ListModel = .init(color: color, isSelect: idx == 0)
            colors.append(model)
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    static var layout: UICollectionViewFlowLayout {
        let layout = UICollectionViewFlowLayout
            .YP.CreateGridLayout(scrollDirection: .horizontal,
                                 minimumLineSpacing: 16,
                                 minimumInteritemSpacing: 0,
                                 sectionInset: .YP.Create(vertical: 6))
        return layout
    }

    var colors: [ListModel] = []

    /// 颜色选择变化
    var colorChangeSelectBlock: YPBlock.OneParam<WmcBaseTagView.Colors>?
    
    /// 选中一个颜色
    func select(color: WmcBaseTagView.Colors) {
        WmcBaseTagView.Colors.allCases.enumerated().forEach { (idx, _color) in
            if color == _color {
                select(at: idx)
                return
            }
        }
    }
    
    /// 选中一个颜色
    func select(at index: Int) {
        for (idx, model) in colors.enumerated() {
            model.isSelect = idx == index
        }
        reloadData()
    }
}

// MARK: - UICollectionViewDelegate

extension WmcTagColorSelectView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = colors.yp.object(at: indexPath.row) {
            select(at: indexPath.row)
            YPDevice.PlayShake(.light)
            let color = model.color
            colorChangeSelectBlock?(color)
        }
    }
}

// MARK: - UICollectionViewDelegateFlowLayout

extension WmcTagColorSelectView: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineSpacing = Self.layout.minimumLineSpacing * (colors.count.yp.cgFloat - 1)
        let horizontalEdge = Self.layout.sectionInset.yp.horizontal
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / colors.count.yp.cgFloat
        let height = collectionView.frame.size.height - Self.layout.sectionInset.yp.vertical
        return CGSize(width: width, height: height)
    }
}

// MARK: - UICollectionViewDataSource

extension WmcTagColorSelectView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colors.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WmcTagColorSelectCell = collectionView.yp.dequeueReusableCell(for: indexPath)
        if let model = colors.yp.object(at: indexPath.row) {
            let colorStr = model.color.info.hexStr
            cell.colorView.backgroundColor = colorStr.yp.uiColor
            Wmc_SpringAnimation() {
                if model.isSelect {
                    cell.colorView.layer.borderWidth = 2
                    cell.transform = CGAffineTransform.YP.CreateScale(1.4)
                } else {
                    cell.colorView.layer.borderWidth = 1
                    cell.transform = CGAffineTransform.YP.CreateScale(1)
                }
            }
        }
        return cell
    }
}
